<html><head><title>CreateMutex</title></head>
<body bgcolor="#EFF1F0" link="#3A3966" vlink="#000000" alink="#000000">
<font face="Verdana, sans-serif" size="2"><p align="center"><b><font size="4">CreateMutex()</font></b></p>
<p><b>Syntax</b></p><blockquote>
Mutex = <font color="#3A3966"><b>CreateMutex</b></font>()</blockquote>

</blockquote>
<b>Description</b><br><blockquote>

Creates a new mutex object. The mutex is initially unlocked. 
<br>
<br>
The main objective of the mutex functions is thread synchronization. They do not create 
too much overhead, but they only work within one program, not system-wide. 
A mutex is an object that can only be &quot;owned&quot; or locked by one thread at a time, 
so it is used to protect shared resources. Only the thread that has the mutex 
locked may access a certain file, memory area, ... 
<br>
See <a href="lockmutex.html">LockMutex()</a> and <a href="unlockmutex.html">UnlockMutex()</a> for locking/unlocking a mutex. 

@noparameter

</blockquote><p><b>Return value</b></p><blockquote>

The new mutext object, or zero if the mutex creation has failed. 

</blockquote><p><b>Example</b></p><blockquote>

<pre><font face="Courier New, Courier, mono"size="2">  <font color="#3A3966">; Run this code once as it is. You will see that the lines printed are</font>
  <font color="#3A3966">; mixed from between the threads. Now uncomment the Mutex functions and</font>
  <font color="#3A3966">; the strings are printed in order, because only one thread at a time</font>
  <font color="#3A3966">; has the right to execute the printing functions.</font>
  <font color="#3A3966">;</font>
  <b><font color="#3A3966">Procedure</font></b> <font color="#3A3966">WithoutMutex</font>(*Number)     
    <b><font color="#3A3966">Shared</font></b> Mutex
    
    <b><font color="#3A3966">For</font></b> a = 1 <b><font color="#3A3966">To</font></b> 5      
      <font color="#3A3966">;LockMutex(Mutex)    ; uncomment this to see the difference</font>
    
<font color="#3A3966">      PrintN</font>(&quot;Thread &quot;+<font color="#3A3966">Str</font>(*Number)+&quot;: Trying to print 5x in a row:&quot;)
      <b><font color="#3A3966">For</font></b> b = 1 <b><font color="#3A3966">To</font></b> 5
<font color="#3A3966">        Delay</font>(50)
<font color="#3A3966">        PrintN</font>(&quot;Thread &quot;+<font color="#3A3966">Str</font>(*Number)+&quot; Line &quot;+<font color="#3A3966">Str</font>(b))
      <b><font color="#3A3966">Next</font></b> b          
      
      <font color="#3A3966">;UnlockMutex(Mutex) ; uncomment this to see the difference</font>
    <b><font color="#3A3966">Next</font></b> a    
  <b><font color="#3A3966">EndProcedure</font></b>
  
<font color="#3A3966">  OpenConsole</font>()
  Mutex =<font color="#3A3966"> CreateMutex</font>()
  
  thread1 =<font color="#3A3966"> CreateThread</font>(@WithoutMutex(), 1)
<font color="#3A3966">  Delay</font>(25)
  thread2 =<font color="#3A3966"> CreateThread</font>(@WithoutMutex(), 2)
<font color="#3A3966">  Delay</font>(25)
  thread3 =<font color="#3A3966"> CreateThread</font>(@WithoutMutex(), 3)
  
<font color="#3A3966">  WaitThread</font>(thread1)
<font color="#3A3966">  WaitThread</font>(thread2)
<font color="#3A3966">  WaitThread</font>(thread3)
  
<font color="#3A3966">  Input</font>()
</font></pre>


</Blockquote><p><b>Supported OS </b><Blockquote>All</Blockquote></p><center><a href="index.html">Thread Index</a> - <a href="createsemaphore.html">CreateSemaphore()</a> -><br><br>

</body></html>